home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / SETENVAR.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  4KB  |  156 lines

  1. /*
  2. **  SETENVAR.C - Program which sets the DOS master environment upon exit
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <conio.h>
  16. #include <dos.h>
  17.  
  18. #if !defined(__ZTC__) && !defined(__TURBOC__)
  19.  #define MK_FP(seg,offset) \
  20.         ((void far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  21.  #define peek(s,o) (*((unsigned far *)(MK_FP(s,o))))
  22.  #define poke(s,o,w) (*((unsigned far *)(MK_FP(s,o)))=(w))
  23. #endif
  24.  
  25. #define SUCCESS 0
  26. #define ERROR -1
  27.  
  28. static unsigned head, tail, start, end;
  29. static int idx = 0;
  30. static unsigned keystack[16][2];
  31.  
  32. /*
  33. **  ungetkey()
  34. **
  35. **  Stuffs characters into the keyboard buffer.
  36. **
  37. **  Parameters: 1 - Extended character to stuff
  38. **
  39. **  Returns: SUCCESS or EOF
  40. **
  41. **  Note: This function assumes that the keyboard buffer is in
  42. **        the normal (for IBM) location of 40:1E.
  43. **
  44. */
  45.  
  46. int ungetkey(unsigned key)
  47. {
  48.         int count;
  49.  
  50. #ifdef __ZTC__
  51.         peek(0x40, 0x1a, &head, sizeof(unsigned));
  52.         peek(0x40, 0x1c, &tail, sizeof(unsigned));
  53.         peek(0x40, 0x80, &start, sizeof(unsigned));
  54.         peek(0x40, 0x82, &end, sizeof(unsigned));
  55. #else
  56.         head  = peek(0x40, 0x1a);
  57.         tail  = peek(0x40, 0x1c);
  58.         start = peek(0x40, 0x80);
  59.         end   = peek(0x40, 0x82);
  60. #endif
  61.         count = tail - head;
  62.         if (0 > count)
  63.                 count += (16 * sizeof(unsigned));
  64.         count >>= 1;
  65.  
  66.         if (15 > count)
  67.         {
  68. #ifdef __ZTC__
  69.                 peek(0x40, tail, &keystack[idx][0], sizeof(unsigned));
  70. #else
  71.                 keystack[idx][0] = peek(0x40, tail);
  72. #endif
  73.                 keystack[idx][1] = tail;
  74. #ifdef __ZTC__
  75.                 poke(0x40, tail, &key, sizeof(unsigned));
  76. #else
  77.                 poke(0x40, tail, key);
  78. #endif
  79.                 tail += sizeof(unsigned);
  80.                 if (end <= tail)
  81.                         tail = start;
  82. #ifdef __ZTC__
  83.                 poke(0x40, 0x1c, &tail, sizeof(unsigned));
  84. #else
  85.                 poke(0x40, 0x1c, tail);
  86. #endif
  87.                 return key;
  88.         }
  89.         return EOF;
  90. }
  91.  
  92. /*
  93. **  KB_stuff()
  94. **
  95. **  Stuffs strings into the keyboard buffer.
  96. **
  97. **  Parameters: 1 - String to stuff
  98. **
  99. **  Returns: SUCCESS if successful
  100. **           ERROR   in case of error, plus beyboard buffer is
  101. **                   restored
  102. **
  103. **  Note: This function assumes that the keyboard buffer is in
  104. **        the normal (for IBM) location of 40:1E.
  105. */
  106.  
  107. int KB_stuff(char *str)
  108. {
  109.         int ercode = SUCCESS;
  110.  
  111.         idx = 0;
  112.         while (*str)
  113.         {
  114.                 if (EOF == ungetkey((unsigned)(*str++)))
  115.                 {
  116.                         while (0 <= --idx)
  117.                         {
  118.                                 tail = keystack[idx][1];
  119. #ifdef __ZTC__
  120.                                 poke(0x40, tail, &keystack[idx][0],
  121.                                         sizeof(unsigned));
  122. #else
  123.                                 poke(0x40, tail, keystack[idx][0]);
  124. #endif
  125.                         }
  126. #ifdef __ZTC__
  127.                         poke(0x40, 0x1c, &tail, sizeof(unsigned));
  128. #else
  129.                         poke(0x40, 0x1c, tail);
  130. #endif
  131.                         ercode = ERROR;
  132.                         break;
  133.                 }
  134.                 else    ++idx;
  135.         }
  136.         idx = 0;
  137.         return ercode;
  138. }
  139.  
  140. void main(int argc, char *argv[])
  141. {
  142.         FILE *bfile;
  143.  
  144.         if (3 > argc)
  145.         {
  146.                 puts("\aUsage: SETENVAR envar datum");
  147.                 abort();
  148.         }
  149.         bfile = fopen("$TMP$.BAT", "w");
  150.         fprintf(bfile, "SET %s=%s\ndel $tmp$.bat\x1a", argv[1], argv[2]);
  151.         fclose(bfile);
  152.         while (kbhit())
  153.                 getch();
  154.         KB_stuff("$tmp$\r");
  155. }
  156.